package org.jeecg.modules.recruit.sign.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.api.dto.req.UserPageReq;
import org.jeecg.api.dto.req.position.WorkSignDto;
import org.jeecg.api.dto.resp.position.MMUserWorkSignResultDto;
import org.jeecg.modules.recruit.appuser.entity.MmAppUser;
import org.jeecg.modules.recruit.appuser.service.IMmAppUserService;
import org.jeecg.modules.recruit.company.entity.MmCompanyMain;
import org.jeecg.modules.recruit.company.service.IMmCompanyMainService;
import org.jeecg.modules.recruit.position.entity.MmCompanyPosition;
import org.jeecg.modules.recruit.position.service.IMmCompanyPositionService;
import org.jeecg.modules.recruit.sign.entity.MmUserSign;
import org.jeecg.modules.recruit.sign.mapper.MmUserSignMapper;
import org.jeecg.modules.recruit.sign.service.IMmUserSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 报名信息
 * @Author: jeecg-boot
 * @Date: 2020-08-24
 * @Version: V1.0
 */
@Service
public class MmUserSignServiceImpl extends ServiceImpl<MmUserSignMapper, MmUserSign> implements IMmUserSignService {

    @Autowired
    private IMmCompanyPositionService mmCompanyPositionService;

    @Autowired
    private IMmAppUserService mmAppUserService;

    @Autowired
    private IMmCompanyMainService mmCompanyMainService;


    @Override
    public boolean workSign(WorkSignDto workSignDto) {
        //获取用户信息
        MmAppUser mmAppUser = mmAppUserService.getById(workSignDto.getUserId());
        if (!Integer.valueOf(1).equals(mmAppUser.getIsAuth())) {
            throw new IllegalArgumentException("请先实名认证");
        }
        // 获取职位信息
        MmCompanyPosition position = mmCompanyPositionService.getById(workSignDto.getPositionId());
        Assert.notNull(position, "职位不存在！异常操作");
        List<MmUserSign> list = this.lambdaQuery().eq(MmUserSign::getUserId, workSignDto.getUserId()).list();
        if (!CollectionUtils.isEmpty(list)) {
            return true;
        }
        MmUserSign mmUserSign = new MmUserSign();
        mmUserSign.setUserId(mmAppUser.getId())
                .setPromotersId(workSignDto.getPositionId())
                .setCompanyId(position.getCompanyId())
                .setSignUserName(mmUserSign.getSignUserName())
                .setSignUserNo(mmUserSign.getSignUserNo())
                .setSignPhone(mmUserSign.getSignPhone());
        this.save(mmUserSign);
        return true;
    }

    @Override
    public Page<MMUserWorkSignResultDto> getUserSignList(UserPageReq req) {
        IPage<MmUserSign> page = this.lambdaQuery()
                .eq(MmUserSign::getUserId, req.getUserId())
                .page(new Page<>(req.getPage(), req.getPageSize()));
        List<MMUserWorkSignResultDto> collect = page.getRecords().stream().map(sign -> {
            MMUserWorkSignResultDto resultDto = new MMUserWorkSignResultDto();
            resultDto.setId(sign.getId());
            resultDto.setCreateTime(sign.getCreateTime());
            resultDto.setPositionId(sign.getPositionId());
            //获取公司
            MmCompanyMain companyMain = mmCompanyMainService.getById(sign.getCompanyId());
            if (companyMain != null) {
                resultDto.setCompanyName(companyMain.getCpName());
                resultDto.setCpPhone(companyMain.getCpPhone());
                resultDto.setCpLocation(companyMain.getCpLocation());
            }
            // 获取职位信息
            MmCompanyPosition position = mmCompanyPositionService.getById(sign.getPositionId());
            if (position != null) {
                resultDto.setSalaryMin(position.getSalaryMin());
                resultDto.setSalaryMax(position.getSalaryMax());
                resultDto.setPsName(position.getPsName());
            }
            return resultDto;
        }).collect(Collectors.toList());
        Page<MMUserWorkSignResultDto> resultDtoPage = new Page<>(page.getCurrent(), req.getPageSize(), page.getTotal());
        resultDtoPage.setRecords(collect);
        return resultDtoPage;
    }


}
